package com.lnsoft.cac.server.handle.codec.frameMessage;


import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import lombok.Data;
import lombok.NonNull;

@Data
/**
 * 配置/状态交互---装置事件参数查询/设置
 * */
public class Cfg_MeasureArg_Cfm {
    private int Command_Status;                 //1byte     数据发送状态：①0xFF成功②0x00失败
    private int Request_Flag;                   //1byte     标识位：标识选择请求或设置的参数，字节位自低向高依次对应10-12的各选择请求或设置的参数项。
    private int PowerAlarm_AutoSend_Flag;       //1byte     停来电告警主动上送标志：①0x00不上送②0x01上送
    private int StartEvent_AutoSend_Flag;       //1byte     复位事件主动上送标志：①0x00不上送②0x01上送
    private int Communication_Flow_Flag;        //1byte     装置通信流量主动上送标志：①0x00不上送②0x01上送

    public ByteBuf toByteBuf() throws Exception{
        if (Command_Status != (byte)0xFF && Command_Status != (byte)0x00) {
            throw new Exception("数据发送状态");
        }
        if (PowerAlarm_AutoSend_Flag != 0x00 && PowerAlarm_AutoSend_Flag != 0x01) {
            throw new Exception("停来电告警主动上送标志");
        }
        if (StartEvent_AutoSend_Flag != 0x00 && StartEvent_AutoSend_Flag != 0x01) {
            throw new Exception("复位事件主动上送标志");
        }
        if (Communication_Flow_Flag != 0x00 && Communication_Flow_Flag != 0x01) {
            throw new Exception("装置通信流量主动上送标志");
        }
        ByteBuf bf = Unpooled.buffer(5);
        bf.writeByte(Command_Status);
        bf.writeByte(Request_Flag);
        bf.writeByte(PowerAlarm_AutoSend_Flag);
        bf.writeByte(StartEvent_AutoSend_Flag);
        bf.writeByte(Communication_Flow_Flag);
        return bf;
    }


    //  解析 { 配置/状态交互---监测参数 -- 确认} 帧报文内容区 到消息体中
    public static Cfg_MeasureArg_Cfm decFrameBody(@NonNull byte[] frameBody) throws Exception {
        ByteBuf bf = Unpooled.wrappedBuffer(frameBody);
        Cfg_MeasureArg_Cfm msg = new Cfg_MeasureArg_Cfm();
        int len = frameBody.length;
        if (len == 0) {
            return null;
        }
        msg.setCommand_Status(bf.getByte(0) & 0xFF);
        msg.setRequest_Flag(bf.getByte(1) & 0xFF);
        msg.setPowerAlarm_AutoSend_Flag(bf.getByte(2) & 0xFF);
        msg.setStartEvent_AutoSend_Flag(bf.getByte(3) & 0xFF);
        msg.setCommunication_Flow_Flag(bf.getByte(4) & 0xFF);
        return msg;
    }

}
